<?php 

require_once 'Doctrine/Common/ClassLoader.php';
require_once 'Sessao.php';


use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger,
    Doctrine\ORM\Mapping\Driver\YamlDriver;

class Doctrine
{
    
    private $doctrineClassLoader;
    private $config;
    private $cache;
    private $driver;
    private $sql_logger;
    private $conexaoOpcoes;
    public $entityManager;


    public function __construct() 
	{
        
        $this->doctrineClassLoader = new ClassLoader('Doctrine\Common',LIBRARY_PATH);
        $this->doctrineClassLoader->register();
        
        $this->doctrineClassLoader = new ClassLoader('Doctrine\DBAL',LIBRARY_PATH);
        $this->doctrineClassLoader->register();
        
        $this->doctrineClassLoader = new ClassLoader('Doctrine\ORM',LIBRARY_PATH);
        $this->doctrineClassLoader->register();
        
        $this->doctrineClassLoader = new ClassLoader('Symfony',LIBRARY_PATH . '/Doctrine');
        $this->doctrineClassLoader->register();
        
        $this->doctrineClassLoader = new ClassLoader('Entities', APPPATH);
        $this->doctrineClassLoader->register();
        
        $this->doctrineClassLoader = new ClassLoader('Proxies', APPPATH);
        $this->doctrineClassLoader->register();
        
        $this->config = new Configuration();
        $this->cache  = new ArrayCache();
        
        $this->config->setMetadataCacheImpl($this->cache);
        $this->config->setQueryCacheImpl($this->cache);
        
        $this->driver = new YamlDriver(APPPATH . 'doctrine/yaml');
        $this->config->setMetadataDriverImpl($this->driver);
        
        $this->config->setProxyDir( APPPATH .'Proxies' );
        $this->config->setProxyNamespace('Proxies');
        
        
        if(LOG_SQL == 1)
        {
            $this->sql_logger = new EchoSQLLogger();
            $this->config->setSQLLogger($this->sql_logger);
            
            //var_dump($this->config->getSQLLogger());
        }
  
        // Configura a auto-geração de classes proxy
		// Se o ambiente for desenvolvimento true, do contrário false
		$this->config->setAutoGenerateProxyClasses( (ENVIRONMENT === 'development') );
        
        // Configura as informações de conexão com o banco de dados
		$this->conexaoOpcoes = array(
                'driver'    => 'pdo_mysql',
                'user'      => 'root',
                'password'  => '',
                'dbname'    => 'cms',
                'host'      => '127.0.0.1'
		);
        
        $this->entityManager = EntityManager::create($this->conexaoOpcoes, $this->config);
        
        var_dump($this->entityManager);
        
        return $this->entityManager;
        
    }
}




?>